package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.RecommendResponse;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-29 16:56
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class ShoppingController {
    @Autowired
    IProductService productService;

    @GetMapping("/product/{id}")
    public ResponseData getProductDetail(@PathVariable("id") Long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = productService.getProductDetail(request);
      //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getProductDetailDto(), "success");
        }
        //失败
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    @GetMapping("/goods")
    public ResponseData getAllProducts(AllProductRequest request) {
        AllProductResponse response = productService.getAllProduct(request);

        //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(response.getProductDtoList());
            pageResponse.setTotal(response.getTotal());
            return new ResponseUtil().setData(pageResponse, "success");
        }
        //失败
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    @GetMapping("/recommend")
    public ResponseData getRecommendGoods() {

        RecommendResponse response = productService.getRecommendGoods();

      //成功
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos(), "success");
        }
        //失败
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }


}
